package jc.malpractice.service;

import cn.hutool.json.JSONObject;
import jc.malpractice.db.DbUtil;
import jc.malpractice.db.Help;

import java.sql.*;
import java.util.ArrayList;

public final class MastService {
    public static JSONObject list(){
        Connection conn = DbUtil.getConn();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            if (conn == null && conn.isClosed()) {
                return null;
            }

            preparedStatement = conn.prepareStatement(
                    "SELECT * FROM event,status_table where event.status = status_table.status_id");
            resultSet = preparedStatement.executeQuery();
            System.out.println(resultSet);
            JSONObject r = Help.Result4Json(resultSet);
            preparedStatement.close();
            r.set("code",0);
            return r;
        }catch(Exception ex){
            return null;
        }
        finally {
            try {
                if (resultSet != null) resultSet.close();
                if (preparedStatement != null) preparedStatement.close();
                conn.close();
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
        }
    }
    public static JSONObject partselect(String status,String keywords,String injury_level,String handler) {
        Connection conn = DbUtil.getConn();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            if (conn == null && conn.isClosed()) {
                return null;
            }
            if(keywords.equals("无")&&injury_level.equals("无")&&status.equals("无")&&handler.equals("无")){
                preparedStatement = conn.prepareStatement(
                        "SELECT * FROM event,status_table where event.status = status_table.status_id");
                resultSet = preparedStatement.executeQuery();
                JSONObject r = Help.Result4Json(resultSet);
                preparedStatement.close();
                r.set("code",0);
                return r;
            }
            else if(keywords.equals("无")&&injury_level.equals("无")&&handler.equals("无")){
                preparedStatement = conn.prepareStatement(
                        "select * from event,status_table where status = ? and event.status = status_table.status_id");
                preparedStatement.setString(1,status);
                resultSet = preparedStatement.executeQuery();
                JSONObject r = Help.Result4Json(resultSet);
                preparedStatement.close();
                r.set("code",0);
                return r;
            }
            else if(status.equals("无")&&injury_level.equals("无")&&handler.equals("无"))
            {
                preparedStatement = conn.prepareStatement(
                        "select * from event,status_table where title=? and event.status = status_table.status_id");
                preparedStatement.setString(1,keywords);
                resultSet = preparedStatement.executeQuery();
                JSONObject r = Help.Result4Json(resultSet);
                preparedStatement.close();
                r.set("code",0);
                return r;
            }
            else if(status.equals("无")&&keywords.equals("无")&&handler.equals("无"))
            {
                preparedStatement = conn.prepareStatement(
                        "select * from event,status_table where injury_level=? and event.status = status_table.status_id");
                preparedStatement.setString(1,injury_level);
                resultSet = preparedStatement.executeQuery();
                JSONObject r = Help.Result4Json(resultSet);
                preparedStatement.close();
                r.set("code",0);
                return r;
            }
            else if(status.equals("无")&&keywords.equals("无")&&injury_level.equals("无"))
            {
                preparedStatement = conn.prepareStatement(
                        "select * from event,status_table where handler_name like '%"+handler+"%' and event.status = status_table.status_id");
                resultSet = preparedStatement.executeQuery();
                JSONObject r = Help.Result4Json(resultSet);
                preparedStatement.close();
                r.set("code",0);
                return r;
            }
            else if(keywords.equals("无")&&injury_level.equals("无")){
                preparedStatement = conn.prepareStatement(
                        "select * from event,status_table where handler_name like '%"+handler+"%' and status=? and event.status = status_table.status_id");
                preparedStatement.setString(1,status);
                resultSet = preparedStatement.executeQuery();
                JSONObject r = Help.Result4Json(resultSet);
                preparedStatement.close();
                r.set("code",0);
                return r;
            }
            else if(keywords.equals("无")&&status.equals("无")){
                preparedStatement = conn.prepareStatement(
                        "select * from event,status_table where handler_name like '%"+handler+"%' and injury_level=? and event.status = status_table.status_id");
                preparedStatement.setString(1,injury_level);
                resultSet = preparedStatement.executeQuery();
                JSONObject r = Help.Result4Json(resultSet);
                preparedStatement.close();
                r.set("code",0);
                return r;
            }
            else if(keywords.equals("无")&&handler.equals("无")){
                preparedStatement = conn.prepareStatement(
                        "select * from event,status_table where injury_level=? and status=? and event.status = status_table.status_id");
                preparedStatement.setString(1,injury_level);
                preparedStatement.setString(2,status);
                resultSet = preparedStatement.executeQuery();
                JSONObject r = Help.Result4Json(resultSet);
                preparedStatement.close();
                r.set("code",0);
                return r;
            }
            else if(injury_level.equals("无")&&status.equals("无")){
                preparedStatement = conn.prepareStatement(
                        "select * from event,status_table where handler_name like '%"+handler+"%' and title=? and event.status = status_table.status_id");
                preparedStatement.setString(1,keywords);
                resultSet = preparedStatement.executeQuery();
                JSONObject r = Help.Result4Json(resultSet);
                preparedStatement.close();
                r.set("code",0);
                return r;
            }
            else if(injury_level.equals("无")&&handler.equals("无")){
                preparedStatement = conn.prepareStatement(
                        "select * from event,status_table where title=? and status=? and event.status = status_table.status_id");
                preparedStatement.setString(1,keywords);
                preparedStatement.setString(2,status);
                resultSet = preparedStatement.executeQuery();
                JSONObject r = Help.Result4Json(resultSet);
                preparedStatement.close();
                r.set("code",0);
                return r;
            }
            else if(status.equals("无")&&handler.equals("无")){
                preparedStatement = conn.prepareStatement(
                        "select * from event,status_table where title=? and injury_level=? and event.status = status_table.status_id");
                preparedStatement.setString(1,keywords);
                preparedStatement.setString(2,injury_level);
                resultSet = preparedStatement.executeQuery();
                JSONObject r = Help.Result4Json(resultSet);
                preparedStatement.close();
                r.set("code",0);
                return r;
            }
            else if(injury_level.equals("无"))
            {
                preparedStatement = conn.prepareStatement(
                        "select * from event,status_table where status=? and title=? and handler_name like '%"+handler+"%' and event.status = status_table.status_id");
                preparedStatement.setString(1,status);
                preparedStatement.setString(2,keywords);
                resultSet = preparedStatement.executeQuery();
                JSONObject r = Help.Result4Json(resultSet);
                preparedStatement.close();
                r.set("code",0);
                return r;
            }
            else if(keywords.equals("无"))
            {
                preparedStatement = conn.prepareStatement(
                        "select * from event,status_table where status=? and injury_level=? and handler_name like '%"+handler+"%' and event.status = status_table.status_id");
                preparedStatement.setString(1,status);
                preparedStatement.setString(2,injury_level);
                resultSet = preparedStatement.executeQuery();
                JSONObject r = Help.Result4Json(resultSet);
                preparedStatement.close();
                r.set("code",0);
                return r;
            }
            else if(status.equals("无"))
            {
                preparedStatement = conn.prepareStatement(
                        "select * from event,status_table where title=? and injury_level=? and handler_name like '%"+handler+"%' and event.status = status_table.status_id");
                preparedStatement.setString(1,keywords);
                preparedStatement.setString(2,injury_level);
                resultSet = preparedStatement.executeQuery();
                JSONObject r = Help.Result4Json(resultSet);
                preparedStatement.close();
                r.set("code",0);
                return r;
            }
            else if(handler.equals("无"))
            {
                preparedStatement = conn.prepareStatement(
                        "select * from event,status_table where title=? and injury_level=? and status=? and event.status = status_table.status_id");
                preparedStatement.setString(1,keywords);
                preparedStatement.setString(2,injury_level);
                preparedStatement.setString(3,status);
                resultSet = preparedStatement.executeQuery();
                JSONObject r = Help.Result4Json(resultSet);
                preparedStatement.close();
                r.set("code",0);
                return r;
            }
            else {
                preparedStatement = conn.prepareStatement(
                        "select * from event,status_table where title=? and injury_level=? and status=? and handler_name like '%"+handler+"%' and event.status = status_table.status_id");
                preparedStatement.setString(1,keywords);
                preparedStatement.setString(2,injury_level);
                preparedStatement.setString(3,status);
                resultSet = preparedStatement.executeQuery();
                JSONObject r = Help.Result4Json(resultSet);
                preparedStatement.close();
                r.set("code",0);
                return r;
            }

        }
        catch (SQLException ex){

            return null;
        }
        finally {
            try {
                if (resultSet != null) resultSet.close();
                if (preparedStatement != null) preparedStatement.close();
                conn.close();
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
        }
    }
    public static boolean check(String reporter_code){
        Connection conn = DbUtil.getConn();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            if (conn == null && conn.isClosed()) {
                return false;
            }

            preparedStatement = conn.prepareStatement(
                    "SELECT is_admin FROM reporter where reporter_code=? ");
            preparedStatement.setString(1,reporter_code);
            resultSet = preparedStatement.executeQuery();
            if(resultSet.next()&&resultSet.getString(1).equals("1"))
                return true;
            else return false;
        }catch(Exception ex){
            return false;
        }
        finally {
            try {
                if (resultSet != null) resultSet.close();
                if (preparedStatement != null) preparedStatement.close();
                conn.close();
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
        }
    }
    public static void main(String[] args){
//        System.out.println(check("ys0403"));
    }
}
